//
//  ChangePhoneNumberController.m
//  DaleTou
//
//  Created by wenrong on 2018/5/29.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "ChangePhoneNumberController.h"
#import "NB_View.h"
#import "AutoTimer.h"
#import "NSString+GNUtil.h"
#import "ShareValue.h"
#import "MBProgressHUD+MJ.h"

@interface ChangePhoneNumberController ()
@property (weak, nonatomic) IBOutlet UITextField *curentPhoneTxtField;
@property (weak, nonatomic) IBOutlet UITextField *verifiedTxtField;
@property (weak, nonatomic) IBOutlet UITextField *nPhoneTxtField;

@property (weak, nonatomic) IBOutlet NB_Button *verifiedBtn;
@property (strong, nonatomic) NSTimer *timer;
@end

@implementation ChangePhoneNumberController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor mainLightGrayColor];
}

- (IBAction)sureBtnClicked:(id)sender {
    if (![NSString isNewPhoneNumber:self.curentPhoneTxtField.text]) {
        [MBProgressHUD showError:@"请输入正确当前手机号"];
        return;
    }
    
    if (![self.curentPhoneTxtField.text isEqualToString:[ShareValue sharedInstance].phoneNum]) {
        [MBProgressHUD showError:@"手机号与当前账号不匹配"];
        return;
    }
    
    if ([self.verifiedTxtField.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"验证码不能为空"];
        return;
    }
    
    if (![NSString isNewPhoneNumber:self.curentPhoneTxtField.text]) {
        [MBProgressHUD showError:@"请输入正确新手机号"];
        return;
    }
    
    [MBProgressHUD showError:@"验证码错误"];
    
}

- (IBAction)verifiedBtnClicked:(id)sender {
    if (![NSString isNewPhoneNumber:self.curentPhoneTxtField.text]) {
        [MBProgressHUD showError:@"请输入正确当前手机号"];
        return;
    }
    if (![self.curentPhoneTxtField.text isEqualToString:[ShareValue sharedInstance].phoneNum]) {
        [MBProgressHUD showError:@"手机号与当前账号不匹配"];
        return;
    }
    
    [self timerWithBtnShow];
    [[AutoTimer sharedInstance] AuotoStart];
}

#pragma mark ----倒计时60秒
- (void)timerWithBtnShow{
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ChangeView) userInfo:nil repeats:YES];
    }
}
- (void)ChangeView{
    
    if ([AutoTimer sharedInstance].isRun) {
        NSString *title = [NSString stringWithFormat:@"%d秒",[AutoTimer sharedInstance].NowTime];
        
        [UIView performWithoutAnimation:^{
            self.verifiedBtn.userInteractionEnabled = NO;
            self.verifiedBtn.backgroundColor = [UIColor grayColor];
            [self.verifiedBtn setTitle:title forState:UIControlStateNormal];
            [self.verifiedBtn layoutIfNeeded];
        }];
    }else{
        [self.timer invalidate];
        self.timer = nil;
        self.verifiedBtn.userInteractionEnabled = YES;
        self.verifiedBtn.backgroundColor = [UIColor colorWithHexString:@"ff8e01"];
        [self.verifiedBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
